#include "../head/recordform.h"
#include "ui_recordform.h"
#include <QPainter>
//#include "../head/utils/databaseutil.h"
#include "../head/utils/exportutil.h"
#include <QMessageBox>

RecordForm::RecordForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RecordForm)
{
    ui->setupUi(this);
    this->setWindowTitle("施工曲线");
    //init_tableview();
    init_data();
}

void RecordForm::init_tableview(){
    //施工曲线
    m_tableModel =new  QStandardItemModel(ui->tableView); //创建模型指定父类
    m_tableModel->setHorizontalHeaderItem(0, new QStandardItem("日期时间"));
    m_tableModel->setHorizontalHeaderItem(1, new QStandardItem("杆长"));
    m_tableModel->setHorizontalHeaderItem(2, new QStandardItem("方位角"));
    m_tableModel->setHorizontalHeaderItem(3, new QStandardItem("倾角"));
    m_tableModel->setHorizontalHeaderItem(4, new QStandardItem("工具角"));
    ui->tableView->horizontalHeader()->setStyleSheet("QHeaderView::section {background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,"
                                                     "stop:0 #cccccc, stop: 0.5 #eeeeee);"
                                                     "color: #333333;padding-left: 4px;border: 1px solid #cccccc;}"
                                                     "QHeaderView::section:checked{background-color: #e6e6e6;}");
    ui->tableView->horizontalHeader()->setMinimumHeight(30);

    ui->tableView->setModel(m_tableModel);
    ui->tableView->setColumnWidth(0,130);
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);  //设置为只读属性


    //设计曲线

    m_tableModel_design =new  QStandardItemModel(ui->tableView); //创建模型指定父类
    m_tableModel_design->setHorizontalHeaderItem(0, new QStandardItem("日期时间"));
    m_tableModel_design->setHorizontalHeaderItem(1, new QStandardItem("杆长"));
    m_tableModel_design->setHorizontalHeaderItem(2, new QStandardItem("方位角"));
    m_tableModel_design->setHorizontalHeaderItem(3, new QStandardItem("倾角"));
    m_tableModel_design->setHorizontalHeaderItem(4, new QStandardItem("工具角"));
    ui->tableView_2->horizontalHeader()->setStyleSheet("QHeaderView::section {background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,"
                                                     "stop:0 #cccccc, stop: 0.5 #eeeeee);"
                                                     "color: #333333;padding-left: 4px;border: 1px solid #cccccc;}"
                                                     "QHeaderView::section:checked{background-color: #e6e6e6;}");
    ui->tableView_2->horizontalHeader()->setMinimumHeight(30);

    ui->tableView_2->setModel(m_tableModel_design);
    ui->tableView_2->setColumnWidth(0,130);
    ui->tableView_2->setEditTriggers(QAbstractItemView::NoEditTriggers);  //设置为只读属性
}

void RecordForm::init_data(){
    if(m_tableModel_design != NULL && m_tableModel != NULL){
        m_tableModel_design->clear();
        m_tableModel->clear();
    }
    init_tableview();
    //读取设计曲线信息
    QString fileName = QApplication::applicationDirPath() + "/conf/shejiquxian.txt";

    QFile file(fileName);
    if(!file.open(QIODevice::ReadOnly)){
        qDebug()<<"打开失败";
    }

    QTextStream in(&file);
    QStringList  user_data;
    int i = 0;

   // m_tableModel_design->clear();
    while(!in.atEnd()){
        QString line = in.readLine();
        user_data = line.split("|");
        for(int j = 0; j < user_data.size() ; j++) {
            m_tableModel_design->setItem(i,j,new QStandardItem(user_data[j].replace("\t","")));
            m_tableModel_design->item(i, j)->setTextAlignment(Qt::AlignCenter);
        }
        i++;
    }
    file.close();



    //读取施工曲线信息
    fileName = QApplication::applicationDirPath() + "/conf/shigongquxian.txt";
    QFile file2(fileName);
    if(!file2.open(QIODevice::ReadOnly)){
        qDebug()<<"打开失败";
    }
    i= 0;
    QTextStream in2(&file2);
 //   m_tableModel->clear();
    while(!in2.atEnd()){
        QString line = in2.readLine();
        user_data = line.split("|");
       // timeList<<user_data.at(0) +" "+ user_data.at(1);

        for(int j = 0; j < user_data.size() ; j++) {
            m_tableModel->setItem(i,j,new QStandardItem(user_data[j].replace("\t","")));
            m_tableModel->item(i, j)->setTextAlignment(Qt::AlignCenter);
        }
        i++;

    }
    file2.close();


}




RecordForm::~RecordForm()
{
    delete ui;
}

//保存数据到Exel
void RecordForm::on_pushButton_2_clicked()
{
    ui->pushButton_2->setText("导出中...");
    ExportUtil exportUtil;
    QString filepath = QFileDialog::getSaveFileName(this, tr("Save as..."),
                                              QString(), tr("EXCEL files (*.xls *.xlsx);;HTML-Files (*.txt);;"));
    exportUtil.export_excel(m_tableModel,filepath);


     QMessageBox::information(this,"提示","导出成功！");
     ui->pushButton_2->setText("导出");

}

void RecordForm::on_pushButton_3_clicked()
{
    QString filepath = QFileDialog::getOpenFileName(this, tr("Save as..."),
                QString(), tr("EXCEL files (*.xls *.xlsx);;HTML-Files (*.txt);;"));

    QAxObject *excel = new QAxObject("ket.Application");
    excel->setControl("ket.Application");// wps程序（需要安装wps）
    //excel->setControl("Excel.Application");// excel程序（需要激活）
    excel->setProperty("Visible", false);       // 不显示窗体
    excel->setProperty("DisplayAlerts", false); // 不显示任何警告信息。如果为true, 那么关闭时会出现类似"文件已修改，是否保存"的提示
    QAxObject* workbooks = excel->querySubObject("WorkBooks");  //获取工作簿集合
    workbooks->dynamicCall("Open(const QString&)", filepath); //打开打开已存在的工作簿
    QAxObject* workbook = excel->querySubObject("ActiveWorkBook"); //获取当前工作簿
    QAxObject* sheet = workbook->querySubObject("WorkSheets(int)", 1);//获取工作表集合的工作表1，即sheet1
    QAxObject* range = sheet->querySubObject("UsedRange"); //获取该sheet的使用范围对象
    QVariant var = range->dynamicCall("Value");
    delete range;
    QVariantList dataList = var.toList();  //得到表格中的所有数据

    workbooks->dynamicCall("Close()"); //关闭工作簿
    excel->dynamicCall("Quit()"); //退出进程

    if (dataList.isEmpty()) {
        return;
    }
    m_tableModel->clear();
    int col = m_tableModel->columnCount();
    for (int i = 1; i < dataList.size(); i++) {
        QVariantList rowData = dataList[i].toList();
        m_tableModel->setItem(i - 1,0,new QStandardItem(rowData[0].toString().replace(10,1," ")));
        m_tableModel->item(i - 1, 0)->setTextAlignment(Qt::AlignCenter);
        m_tableModel->setItem(i - 1,1,new QStandardItem(rowData[1].toString()));
        m_tableModel->item(i - 1, 1)->setTextAlignment(Qt::AlignCenter);
        m_tableModel->setItem(i - 1,2,new QStandardItem(rowData[2].toString()));
        m_tableModel->item(i - 1, 2)->setTextAlignment(Qt::AlignCenter);
        m_tableModel->setItem(i - 1,3,new QStandardItem(rowData[3].toString()));
        m_tableModel->item(i - 1, 3)->setTextAlignment(Qt::AlignCenter);
        m_tableModel->setItem(i - 1,4,new QStandardItem(rowData[4].toString()));
        m_tableModel->item(i - 1, 4)->setTextAlignment(Qt::AlignCenter);
    }
}

//添加一条设计曲线记录
void RecordForm::add_design(QString _CourseAngle_fy,QString _PitchAngle_fy,QString zgcd){

    int count = m_tableModel_design->rowCount();
    QDateTime dateTime = QDateTime::currentDateTime();

    m_tableModel_design->setItem(count,0,new QStandardItem(dateTime.toString("yyyy-MM-dd hh:mm:ss")));
    m_tableModel_design->item(count, 0)->setTextAlignment(Qt::AlignCenter);
    m_tableModel_design->setItem(count,1,new QStandardItem(zgcd));
    m_tableModel_design->item(count, 1)->setTextAlignment(Qt::AlignCenter);
    m_tableModel_design->setItem(count,2,new QStandardItem(_CourseAngle_fy));
    m_tableModel_design->item(count, 2)->setTextAlignment(Qt::AlignCenter);
    m_tableModel_design->setItem(count,3,new QStandardItem(_PitchAngle_fy));
    m_tableModel_design->item(count, 3)->setTextAlignment(Qt::AlignCenter);
    m_tableModel_design->setItem(count,4,new QStandardItem(""));
    m_tableModel_design->item(count, 4)->setTextAlignment(Qt::AlignCenter);

    QString fileName = QApplication::applicationDirPath() + "/conf/shejiquxian.txt";

    QFile file(fileName);

     if(!file.open(QFile::WriteOnly | QFile::Truncate)){
         qDebug()<<"打开失败";
     }
      QTextStream out(&file);
     int rows = m_tableModel_design->rowCount();
     for(int i=0;i<rows;i++){
         out <<  m_tableModel_design->item(i,0)->text()<<"|"<<
                 m_tableModel_design->item(i,1)->text()<<"|"<<
                  m_tableModel_design->item(i,2)->text()<<"|"<<
                  m_tableModel_design->item(i,3)->text()<<endl;

     }
     file.close();
}

//添加一条施工曲线记录
void RecordForm::add_sgqx(QString _CourseAngle_fy,QString _PitchAngle_fy,QString zgcd){
    int count = m_tableModel->rowCount();
    QDateTime dateTime = QDateTime::currentDateTime();

    m_tableModel->setItem(count,0,new QStandardItem(dateTime.toString("yyyy-MM-dd hh:mm:ss")));
    m_tableModel->item(count, 0)->setTextAlignment(Qt::AlignCenter);
    m_tableModel->setItem(count,1,new QStandardItem(zgcd));
    m_tableModel->item(count, 1)->setTextAlignment(Qt::AlignCenter);
    m_tableModel->setItem(count,2,new QStandardItem(_CourseAngle_fy));
    m_tableModel->item(count, 2)->setTextAlignment(Qt::AlignCenter);
    m_tableModel->setItem(count,3,new QStandardItem(_PitchAngle_fy));
    m_tableModel->item(count, 3)->setTextAlignment(Qt::AlignCenter);
    m_tableModel->setItem(count,4,new QStandardItem(""));
    m_tableModel->item(count, 4)->setTextAlignment(Qt::AlignCenter);


    QString fileName = QApplication::applicationDirPath() + "/conf/shigongquxian.txt";

    QFile file(fileName);
     if(!file.open(QFile::WriteOnly | QFile::Truncate)){
         qDebug()<<"打开失败";
     }
      QTextStream out(&file);
     int rows = m_tableModel->rowCount();
     for(int i=0;i<rows;i++){
         out <<  m_tableModel->item(i,0)->text()<<"|"<<
                 m_tableModel->item(i,1)->text()<<"|"<<
                  m_tableModel->item(i,2)->text()<<"|"<<
                  m_tableModel->item(i,3)->text()<<endl;

     }
     file.close();
}

void RecordForm::on_pushButton_5_clicked()
{
    ui->pushButton_5->setText("导出中...");
    ExportUtil exportUtil;
    QString filepath = QFileDialog::getSaveFileName(this, tr("Save as..."),
                                              QString(), tr("EXCEL files (*.xls *.xlsx);;HTML-Files (*.txt);;"));
    exportUtil.export_excel(m_tableModel_design,filepath);


     QMessageBox::information(this,"提示","导出成功！");
     ui->pushButton_5->setText("导出");
}
